var devCode=getQueryString("devCode");
var url='../devchangerecord/list';
if(devCode!=null){
    url='../devchangerecord/list?devCode='+devCode
}
$(function () {
    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', align: 'center', key: true, hidden: true},
            {label: '设备编码', name: 'devCode', index: 'DEV_CODE', width: 80, align: 'center'},
            {label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 80, align: 'center'},
            {
                label: '设备状态', name: 'devStatus', index: 'DEV_STATUS', width: 80, align: 'center',
                formatter: function (value) {
                    return sysEnums.STATUS[value];
                }
            },
            {label: '所属车间', name: 'workshopName', index: 'workshopName', width: 80, align: 'center'},
            {label: '生产线名称', name: 'devProductName', index: 'DEV_PRODUCT_NAME', width: 80, align: 'center'},
            {label: '工序名称', name: 'devProceName', index: 'DEV_PROCE_NAME', width: 80, align: 'center'},
            {label: '操作时间', name: 'createDate', index: 'CREATE_DATE', width: 80, align: 'center',
                formatter:function (value) {
                    return transDate(value,'yyyy-MM-dd hh:mm')
                }},
            {label: '操作人', name: 'createUser', index: 'CREATE_USER', width: 80, align: 'center'},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80, align: 'center'}
        ]
    });
});
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
};

var deptTree;
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devChangeRecord: {
            isDel: '',
            checkReason: '',
            checkResult: '',
            checkDate: '',
            useDept: '',
            devProceName: '',
            devProceNo: '',
            devProductName: '',
            devStatus: '',
            status: '',
            devCode: '',
            devName: '',
            id: '',
            updateDate: '',
            updateUser: '',
            createUser: '',
            createDate: '',
            devProducName:'',
            workshopName:'',
        },
        ruleValidate: {
            devCode: [
                {required: true, message: '设备编码不能为空', trigger: 'blur'}
            ],
            devName: [
                {required: true, message: '设备名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            devProductName:'',
            devProceName:'',
            devName:''
        },
        devCodeList: [],
        statusList: enumsToList('STATUS'),
        statusList2: enumsToList('STATUS2'),
        isSpecList: enumsToList('SPEC_TYPE'),

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devChangeRecord = {
                isDel: '1',
                checkReason: '',
                checkResult: '',
                checkDate: '',
                useDept: '',
                devProceName: '',
                devProceNo: '',
                devProductName: '',
                devStatus: 1,
                status: 1,
                devCode: '',
                devName: '',
                id: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
                subFactory: '',
                workshop: '',
                remark: '',
                devProducName:'',
                workshopName:'',
            };
            vm.getDevCodeList()
            vm.getDeptTree()
            if(devCode!=null){
                Ajax.request({
                    url: '../deviceinfo/queryAll',
                    params: {devCode: devCode},
                    async: true,
                    successCallback: function (r) {
                        vm.devChangeRecord.devCode = r.list[0].devCode;
                        vm.devChangeRecord.devName = r.list[0].devName;
                        vm.devChangeRecord.devId = r.list[0].id;
                        vm.devChangeRecord.devProductName = r.list[0].devProductName;//生产线名称
                        vm.devChangeRecord.devProceNo = r.list[0].devProceNo;//工序号
                        vm.devChangeRecord.workshop = r.list[0].devFarm;//所属车间
                    }
                });
            }
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.devChangeRecord = {
                isDel: '',
                checkReason: '',
                checkResult: '',
                checkDate: '',
                useDept: '',
                devProceName: '',
                devProceNo: '',
                devProductName: '',
                devStatus: '',
                devCode: '',
                devName: '',
                id: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
                subFactory: '',
                workshop: '',
                remark: ''
            };
            vm.getInfo(id);
            vm.getDevCodeList()
            vm.getDeptTree()
        },
        saveOrUpdate: function (event) {
            //设备信息变更
            var url = vm.devChangeRecord.id == (null || '') ? "../devchangerecord/save" : "../devchangerecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devChangeRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRow("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devchangerecord/delete/" + ids,
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../devchangerecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devChangeRecord = r.devChangeRecord;
                }
            });
        },
        getDevCodeList: function () {
            Ajax.request({
                url: "../deviceinfo/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.devCodeList = r.list;
                }
            });
        },
        //数据返填
        getDevCodeName: function (devCode) {
            if (devCode) {
                Ajax.request({
                    url: "../deviceinfo/getInfoByDevCode/" + devCode,
                    type: 'POST',
                    successCallback: function (r) {
                        vm.devChangeRecord.devName = r.deviceInfo.devName
                        vm.devChangeRecord.devProductName = r.deviceInfo.devProductName    //生产线名称 devProductName
                        vm.devChangeRecord.devProceNo = r.deviceInfo.devProceNo   //工序号 devProceNo
                        vm.devChangeRecord.devProceName = r.deviceInfo.devProceName  //工序名称 devProceName
                        vm.devChangeRecord.devCode = r.deviceInfo.devCode//设备编号
                    }
                });
            }
        },
        viewInfo: function (id) {
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                devProductName:'',
                devProceName:'',
                devName:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        //显示设备列表-使用设备码
        getDeviceInfo: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../sys/deviceInfoSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.devChangeRecord.devCode = res.devCode;
                        vm.devChangeRecord.devName = res.devName;
                        vm.devChangeRecord.devId = res.id;
                        vm.devChangeRecord.devProductName = res.devProductName;//生产线名称
                        vm.devChangeRecord.devProceNo = res.devProceNo;//工序号
                        vm.devChangeRecord.workshop = res.devFarm;//所属车间
                    }
                }
            })
        },
        review: function() {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            vm.getInfo(ids);
            vm.devChangeRecord.devStatus=rows.devStatus=='闲置'?2:rows.devStatus=='报废'?0:rows.devStatus=='完好在用'?1:3
            openWindow({
                title: "设备状态",
                area: ['550px', '300px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    Ajax.request({
                        url: '../devchangerecord/review',
                        params: JSON.stringify(vm.devChangeRecord),
                        type: 'POST',
                        contentType: "application/json",
                        successCallback: function (result) {
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        }
                    });
                }
            });
        },
        /**
         * 生产线获取
         */
        getDeptTree: function () {
            //加载分类树
            Ajax.request({
                url: '../sys/user/treeDeptByType/DT_CJKSCX',
                async: false,
                successCallback: function (r) {
                    deptTree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    deptTree.expandAll(true);
                    var node = deptTree.getNodeByParam("deptId", vm.devChangeRecord.workshop);
                    if (node) {
                        deptTree.selectNode(node);
                        vm.devChangeRecord.workshopName = node.name;
                    } else {
                        node = deptTree.getNodeByParam("deptId", "0");
                        deptTree.selectNode(node);
                    }
                }
            });
        },
        deptTreeShow: function () {
            openWindow({
                title: "选择生产线",
                area: ['300px', '450px'],
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = deptTree.getSelectedNodes();
                    vm.devChangeRecord.workshop = node[0].parentId;
                    vm.devChangeRecord.workshopName = node[0].parentName;
                    if (node[0].deptType == 'DT_CJKSCX') {
                        // //选择上级菜单
                        vm.devChangeRecord.devProductName = node[0].deptId;
                        vm.devChangeRecord.devProducName = node[0].name;
                        layer.close(index);
                    }
                    /*
                    var node = deptTree.getSelectedNodes();
                    if (node[0].deptType == 'DT_ZZCJ') {
                        // //选择上级菜单
                        vm.devChangeRecord.workshop = node[0].deptId;
                        vm.devChangeRecord.workshopName = node[0].name;
                        console.log(vm.devChangeRecord);
                        layer.close(index);*/
                    }
                });
        },
    }
});
